package com.itzz.ssm.service.impl;




import com.itzz.ssm.domain.SysUser;
import com.itzz.ssm.mapper.RoleMapper;
import com.itzz.ssm.mapper.SysUserMapper;
import com.itzz.ssm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:liuweibo
 * @Date Create in 2021/8/10 14:47
 * @Description:
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public SysUser findUserNameAndPassword(SysUser sysUser) {
        return sysUserMapper.findUserNameAndPassword(sysUser);
    }

    @Override
    public List<SysUser> findSysUser() {
        return sysUserMapper.findSysUser();
    }

    @Transactional
    @Override
    public void deleteSysUserById(int id) {
        sysUserMapper.deleteSysUserRole(id);//先删除关系表
        sysUserMapper.deleteSysUserById(id);//后删除用户表
    }

    /*添加用户*/
    @Transactional

    public void addSysUser(SysUser sysUser,int[] roleId) {
        /*添加用户 获取插入数据的自增id*/
        sysUserMapper.addSysUser(sysUser);
        System.out.println(sysUser.getId());
        /*添加用户角色关系*/
        roleMapper.addRoleUsers(sysUser.getId(),roleId);
    }


}
